//
//  AccountController.m
//  WeChat
//
//  Created by wpapple on 16/6/15.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "AccountController.h"
#import "AppDelegate.h"
#import "ViewController.h"
#import "WPXMPPTools.h"


@interface AccountController()

@property (weak, nonatomic) IBOutlet UITextField *enterAccount;
@property (weak, nonatomic) IBOutlet UITextField *enterPassword;
@property (strong , nonatomic) ViewController * viewController;

@end

@implementation AccountController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    UILabel * accountLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [accountLabel setText:@"账号:"];
    
    UILabel * passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [passwordLabel setText:@"密码:"];
    
    self.enterPassword.leftView = passwordLabel;
    self.enterPassword.leftViewMode = UITextFieldViewModeAlways;
    
    self.enterAccount.leftView = accountLabel;
    self.enterAccount.leftViewMode = UITextFieldViewModeAlways;
    
    
}

- (IBAction)login:(UIButton *)sender {
    
//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    [app goOffline];
    
    WPUserInfo * wpUserInfo = [WPUserInfo sharedWPUserInfo];
    
    wpUserInfo.user = @"zhangsan";
    wpUserInfo.pwd = @"123456";
    
    __weak typeof(self) w_self = self;
    [[WPXMPPTools sharedWPXMPPTools] xmppLogin:^(XMPPState state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [w_self dealXMPPSate:state];
        });
    }];
    
}

-(void)dealXMPPSate:(XMPPState)state{
    switch (state) {
        case XMPPLoginSuccess:
        {
            NSLog(@"登录成功");
            
            //            UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            //            self.view.window.rootViewController = storyboard.instantiateInitialViewController;
            
//            [self.viewController dismissViewControllerAnimated:YES completion:nil];
            
        }
            
            break;
            
        case XMPPLoginFailure:{
            NSLog(@"登录失败");
            [MBProgressHUD showError:@"账号或者密码错误" toView:self.view];
        }
            
        default:
            break;
    }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    self.viewController = [segue destinationViewController];
    
}


@end
